
namespace AlertMail
{
    using System;
    using System.Data;

    interface INewsletter
    {
        void SendEmailToUsers();
    }

    public class Newsletter : INewsletter
    {
        public Newsletter()
        {
         
        }
         
        public void SendEmailToUsers()
        {
            try
            {
                SqlServerProvider sqlServer = new
                SqlServerProvider();
                sqlServer.CommandText =
                "SPGetEmailsForNewsletter";
                sqlServer.CommandType =  
                CommandType.StoredProcedure;
                DataTable dataTableAlert =
                sqlServer.GetDataTable();                
                string emailAddress = "";
                               
                if (dataTableAlert.Rows.Count > 0)
                {
                    for (int i = 0; i < dataTableAlert.Rows.Count; i++)
                    {                         
                        emailAddress = dataTableAlert.Rows[i]["Email"].ToString();
                        if (emailAddress != "")
                            SendMailToUser(emailAddress, "Newsletter", CreateContentForNewsletter());
                    }
                }
                sqlServer.CloseConnection();
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Newsletter::SendEmails", ex);
            }
        }
        void SendMailToUser(string emailTo, string subject, string contentToSend)
        {        
            MailProvider mailProvider = new MailProvider();
            if(mailProvider.IsEmail(emailTo))
                mailProvider.SendMail(emailTo, subject, contentToSend);
        }
        string CreateContentForNewsletter()
        {
            string siteUrl = "<a href=\"http://"+
            "www.recruitvietnam.com/" +
            "jobseekers/jobdetails.aspx?";
            string content = "Dear \n";
            content += "RecruitVietnam would "+
            "like to announce new service "+
            "open in Jan 1 2008 \n" + siteUrl;             
            return content;
        }
         
    }
}
